﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using MaxStudio.Concrete.Web;
using MaxStudio.Concrete.DBUtility;
using MaxStudio.Concrete.Server;
using MaxStudio.Concrete.Server.Service;
using MaxStudio.Concrete.Server.Dao;
using MaxStudio.Concrete.Component;

namespace MaxStudio.Concrete.Web.Pages
{
    public partial class RM : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindDDL();
                BindVenderList();
                BindGridView();
            }
        }

        #region Private Method
        private void BindVenderList()
        {
            RMVenderService rMVenderService = ServiceFactory.getRMVenderService();
            OperationResult<List<tb_RMVender>> result = rMVenderService.GetAllRMVender();

            if (result.operationSuccess)
            {
                ddlVender.DataSource = result.obj;
                ddlVender.DataTextField = "Name";
                ddlVender.DataValueField = "Id";
                ddlVender.DataBind();
            }
            else
            {
                //EXCEPTION
                //return null;
            }
        }

        private void BindEditVenderList()
        {
            RMVenderService rMVenderService = ServiceFactory.getRMVenderService();
            OperationResult<List<tb_RMVender>> result = rMVenderService.GetAllRMVender();

            if (result.operationSuccess)
            {
                ddlEditVender.DataSource = result.obj;
                ddlEditVender.DataTextField = "Name";
                ddlEditVender.DataValueField = "Id";
                ddlEditVender.DataBind();
            }
            else
            {
                //EXCEPTION
                //return null;
            }
        }

        private void BindDDL()
        {
            WebPartBind.BindMasterDataDDL(this.ddlType, Int32.Parse(System.Configuration.ConfigurationSettings.AppSettings["MDRMType"].ToString()));   
        }

        private void BindEditDDL()
        {
            WebPartBind.BindMasterDataDDL(this.ddlEditType, Int32.Parse(System.Configuration.ConfigurationSettings.AppSettings["MDRMType"].ToString()));
        }

        private void BindGridView()
        {
            RMService rMService = ServiceFactory.getRMService();
            OperationResult<List<tb_RM>> result = rMService.GetAllRM();

            if (result.operationSuccess)
            {
                gvRMList.DataSource = result.obj;
                gvRMList.DataBind();
            }
            else
            {
                //EXCEPTION
                //return null;
            }
        }

        private void BindEdieDialog(int id)
        {
            BindEditDDL();
            BindEditVenderList();
            RMService RMServiceObj = ServiceFactory.getRMService();

            OperationResult<tb_RM> resultEdit = RMServiceObj.GetRMById(id);
            if (resultEdit.operationSuccess == true)
            {
                ddlEditType.SelectedValue = resultEdit.obj.TypeId.ToString();
                ddlEditVender.SelectedValue = resultEdit.obj.BelongVender.ToString();
                txtEditName.Text = resultEdit.obj.Name;
                txtEditCode.Text = resultEdit.obj.Code;
                txtEditSN.Text = resultEdit.obj.SN;
                txtEditCount.Text = resultEdit.obj.Count.ToString();
                txtEditPrice.Text = resultEdit.obj.Price.ToString();
                txtEditDescription.Text = resultEdit.obj.Description;
                lbLastModier.Text = "TEST";
                lbEidtId.Text = resultEdit.obj.Id.ToString();
                lbLastModifyDate.Text = resultEdit.obj.ModifyDate.ToShortDateString(); 
            }
        }

        private void DeleteRecords(int id)
        {
            lbDeleteId.Text = id.ToString();
        }

        public string BindVenderNameByVenderId(string venderId)
        {
            string strReturnValue = "";
            OperationResult<tb_RMVender> result = ServiceFactory.getRMVenderService().GetRMVenderById(Int32.Parse(venderId));
            if (result.operationSuccess)
            {
                strReturnValue = result.obj.Name;
            }
            return strReturnValue;
        }

        public string GetMasterDataNameById(string strTypeId)
        {
            int masterDataId = Int32.Parse(strTypeId);
            return MasterDateString.GetMasterDataNameById(masterDataId);
        }  

        #region Insert NeedPay
        
        public void CreateNeedPay(tb_NeedPay needPayInstance)
        {
            OperationResult<tb_NeedPay> result = ServiceFactory.getNeedPayService().CreateNeedPay(needPayInstance); 
        }

        #endregion

        #endregion

        #region Event
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            RMService RMServiceObj = ServiceFactory.getRMService();
            tb_RM instance = new tb_RM(); 

            instance.TypeId = Int32.Parse( ddlType.SelectedValue.ToString ());
            instance.BelongVender = Int32.Parse( ddlVender.SelectedValue.ToString ());
            instance.Name= txtName.Text;
            instance.Code= txtCode.Text;
            instance.SN=txtSN.Text;
            instance.Count =decimal.Parse( txtCount.Text);
            instance.Price=decimal.Parse( txtPrice.Text);
            instance.Description = txtDescription.Text;

            OperationResult<tb_RM> result = RMServiceObj.CreateRM(instance);

            tb_NeedPay needPayInstance =new tb_NeedPay ();
            needPayInstance.Amount= instance.Price*(decimal)instance.Count;
            needPayInstance.VenderId=instance.BelongVender;
            needPayInstance.Description=instance.Name+";"+instance.Code;
            needPayInstance.Status = 38; // hard code
            needPayInstance.Tracking = 1;//hard code
            CreateNeedPay(needPayInstance);
            BindGridView(); 

        }
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            int editId = 0;
            if (lbEidtId.Text.Length > 0)
            {
                editId = Int32.Parse(lbEidtId.Text);
            }

            RMService RMServiceObj = ServiceFactory.getRMService(); 
            tb_RM entity = new tb_RM();

            entity.Id = editId;
            entity.TypeId = Int32.Parse(ddlEditType.SelectedValue.ToString());
            entity.BelongVender = Int32.Parse(ddlEditVender.SelectedValue.ToString());
            entity.Name = txtEditName.Text;
            entity.Code = txtEditCode.Text;
            entity.SN = txtEditSN.Text;
            entity.Count = decimal.Parse(txtEditCount.Text);
            entity.Price = decimal.Parse(txtEditPrice.Text);
            entity.Description = txtEditDescription.Text;
            entity.ModifyDate = DateTime.Now;
            entity.ModifyBy = 1;
            entity.CreateDate = DateTime.Now;

            OperationResult<tb_RM> resultEdit = RMServiceObj.ModifyRM(entity); 
            BindGridView(); 
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int deleteId = Int32.Parse(lbDeleteId.Text);
            RMService RMServiceObj = ServiceFactory.getRMService();
            tb_RM deleteEntity = new tb_RM();
            deleteEntity.Id = deleteId;
            RMServiceObj.DeleteRM(deleteEntity);
            BindGridView();
        }

        protected void gvRMList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                string strSelectId = e.CommandArgument.ToString();
                BindEdieDialog(Int32.Parse(strSelectId));
            }

            if (e.CommandName == "del")
            {
                string strSelectId = e.CommandArgument.ToString();
                DeleteRecords(Int32.Parse(strSelectId));
            }
        }

        protected void gvRMList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvRMList.PageIndex = e.NewPageIndex;
            BindGridView();
        }
      
        #endregion
    }
}

